package com.og.ouroa.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.annotations.EnableLoginHandle;
import com.og.ouroa.common.exception.BusinessException;
import com.og.ouroa.common.result.R;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.Course;
import com.og.ouroa.pojo.entity.CourseFile;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.CourseFileVO;
import com.og.ouroa.service.CourseFileService;
import com.og.ouroa.service.FileLoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/courseFile")
@Api(tags = "课程资料接口")
public class CourseFileController {

    @Autowired
    CourseFileService courseFileService;

    @Autowired
    FileLoadService fileLoadService;


    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("根据资料标题与课程id删除资料(老师权限)")
    @DeleteMapping("remove/{fileName}/{cid}")
    public R removeByName(@ApiParam(value = "资料标题" )@PathVariable String fileName,
                          @ApiParam(value = "课程id" )@PathVariable Integer cid){


        fileLoadService.remove( courseFileService.reomveByName( fileName, cid));
        return R.ok().message("删除成功");
    }
    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("根据资料主键id删除资料(老师权限)")
    @DeleteMapping("remove/{fid}")
    public R removeByid( @ApiParam(value = "表主键id" )@PathVariable Integer fid){



        fileLoadService.remove(courseFileService.reomveFid( fid));

        return R.ok().message("删除成功");
    }



    @EnableLoginHandle
    @ApiOperation("根据课程号获取所有资料")
    @GetMapping("getbycid/{page}/{limit}/{cid}")
    public R getByCid( @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                       @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                       @ApiParam(value = "课程id" )@PathVariable Integer cid){


        Page<CourseFile> pages = new Page<>(page, limit);
        return  R.ok().message("查询成功").data("page",courseFileService.listByCid(pages,cid));
    }

    @EnableLoginHandle
    @ApiOperation("根据工号获取所属资料")
    @GetMapping("getbysid/{page}/{limit}/{sid}")
    public R getBySid( @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                       @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                       @ApiParam(value = "用户id" )@PathVariable Integer sid){


        Page<CourseFile> pages = new Page<>(page, limit);
        return  R.ok().message("查询成功").data("page",courseFileService.listBySid(pages,sid));
    }

    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("文件保存(老师权限)")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "文件",required = true)
            @RequestParam("file")
                    MultipartFile file,
            @ApiParam(value = "对应课程", required = true)
            @RequestParam("cid")
                    Integer cid,
            @ApiParam(value = "作者", required = true)
            @RequestParam("sid")
                    Integer sid){

        String url = null;
        String Filename;
        try {
            InputStream inputStream = file.getInputStream();
             Filename = file.getOriginalFilename();
            url = fileLoadService.upload(inputStream,"coursefile" , Filename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR,e);

        }
        CourseFileVO vo = new CourseFileVO();
        vo.setFile(url);
        vo.setSid(sid);
        vo.setCourseId(cid);
        vo.setFileName(Filename);
        courseFileService.insert(vo);

        return R.ok().message("文件存储成功").data("CourseFileVO",vo);

    }
}

